using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TeamFoundationServerSensor;

public partial class _Settings : System.Web.UI.Page {

    /// <summary>
    /// Load the default path addresses to allow for changing.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e) {
        if (!this.Page.IsPostBack) {
            Refresh();
            CheckBisSubscribe(sender, e);
            CheckWindowsLogin(sender, e);
        }
    }

    /// <summary>
    /// Save the settings on the page if possible.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Save_Click(object sender, EventArgs e) {

        status.Text = "";

        // Only try to update if validation has passed.
        if (this.IsValid) {

            // Make sure to save UseFile first otherwise we may not correctly save 
            // to the database.
            Settings.Instance.saveSetting("UseFile", UseFile.Checked.ToString());
            Settings.Instance.saveSetting("UseIEvent", UseIEvent.Checked.ToString());
            Settings.Instance.saveSetting("BisSubscribe", PathBisSubscribe.Text);

            Settings.Instance.saveSetting("Address", PathAddress.Text);
            Settings.Instance.saveSetting("Server", PathServer.Text);

            Settings.Instance.saveSetting("UseWindowsSQLLogin", this.UseWindowsSQLLogin.Checked.ToString());
            Settings.Instance.saveSetting("TFSUser", this.SQLUsername.Text);
            Settings.Instance.saveSetting("TFSPassword", this.SQLPassword.Text);

            // Always need to save some settings to file even if using database.
            if (Settings.Instance.save()) {
                status.Text = "Saved: " + DateTime.Now;
            }
            else {
                status.Text = "Could not save settings.";
            }
        }

        Refresh();
    }

    /// <summary>
    /// Refresh setting contents on the page.
    /// </summary>
    private void Refresh() {

        this.UseFile.Checked = Settings.Instance.UseFile;

        this.UseIEvent.Checked = Settings.Instance.getSettingBoolean("UseIEvent");
        this.PathBisSubscribe.Text = Settings.Instance.getSetting("BisSubscribe");

        this.UseWindowsSQLLogin.Checked = Settings.Instance.getSettingBoolean("UseWindowsSQLLogin");
        this.SQLUsername.Text = Settings.Instance.getSetting("TFSUser");
        this.SQLPassword.Text = Settings.Instance.getSetting("TFSPassword");

        this.PathAddress.Text = Settings.Instance.getSetting("Address");
        this.PathServer.Text = Settings.Instance.getSetting("Server");
    }

    /// <summary>
    /// Checks to make sure the BisSubscribe.exe file actually exists.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e">The path to check.</param>
    protected void PathBisSubscribeValidate(object sender, ServerValidateEventArgs e) {
        
        // Check that the file exists and can be executed.
        if (FormValidator.FileAccessible(e.Value)) {
            e.IsValid = true;
        }
        else {
            e.IsValid = false;
        }
    }

    /// <summary>
    /// Checks to make sure the address parameter points to a directory and not
    /// a file.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e">The path to check.</param>
    protected void PathAddressValidate(object sender, ServerValidateEventArgs e) {

        // Check that the last character is an '/'
        if (e.Value.Length - 1 == e.Value.LastIndexOf('/')) {
            e.IsValid = true;
        }
        else {
            e.IsValid = false;
        }
    }

    /// <summary>
    /// Enable or disable the BisSubscribe field depending on if we are using
    /// the IEventService or not.
    /// </summary>
    protected void CheckBisSubscribe(object sender, EventArgs e) {

        // Check whether the IEventService is checked or not.
        if (UseIEvent.Checked) {
            ShowBisSubscribe.Visible = false;
        }
        else {
            ShowBisSubscribe.Visible = true;
        }
    }

    /// <summary>
    /// Enable or disable the ShowWindowsSQL* fields depending on if we are 
    /// using the Windows login or not.
    /// </summary>
    protected void CheckWindowsLogin(object sender, EventArgs e) {

        // Check whether the user wishes to use WindowsSQL authentication or not.
        if (UseWindowsSQLLogin.Checked) {
            ShowWindowsSQLLogin.Visible = false;
            ShowWindowsSQLPass.Visible = false;
        }
        else {
            ShowWindowsSQLLogin.Visible = true;
            ShowWindowsSQLPass.Visible = true;
        }
    }
}
